package com.solidtrust.admin.controller;


import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.solidtrust.app.ProductAPI;
import com.solidtrust.app.ProductAPI.Product;
import com.solidtrust.entity.AccessItem;
import com.solidtrust.entity.Client;
import com.solidtrust.repository.AccessRepository;



@Controller
@Transactional(readOnly = true)
@RequestMapping(value = "/account")
public class AccountController {

	Logger log4j = Logger.getLogger(AccountController.class);
	
	@PersistenceContext
	private EntityManager em;
	@Autowired
	private AccessRepository accessRep;
	
	@RequestMapping(value = "/index.html")
	public String list(Model model){
		model.addAttribute("list", em.createQuery("from Client", Client.class).getResultList());
		return "admin/account/list.jsp";
	}
	
	@Transactional(readOnly = false)
	@RequestMapping(value = "/save")
	public String save(Client c){
		em.merge(c);
		return "redirect:/account/index.html/#" + c.getId();
	}
	
	
	@Transactional(readOnly = false)
	@RequestMapping(value = "/remove")
	public String revmoe(@RequestParam("id") int id){
		Client c = em.find(Client.class, id);
		em.remove(c);
		return "redirect:/account/index.html";
	}
	
	@Transactional(readOnly = false)
	@RequestMapping(value = "/ip")
	public String changeIP(@RequestParam("id") int id, @RequestParam("ip") String ip){
		Client c = em.find(Client.class, id);
		c.setIp(ip);
		return "redirect:/account/index.html/#" + c.getId();
	}
	
	@Transactional(readOnly = false)
	@RequestMapping(value = "/token")
	public String changeToken(@RequestParam("id") int id, @RequestParam("token") String token){
		Client c = em.find(Client.class, id);
		c.setToken(token);
		return "redirect:/account/index.html/#" + c.getId();
	}
	
	@RequestMapping(value = "/visit/detail")
	public String visits(@RequestParam("id") String id, @RequestParam("productId") Integer productId, 
			@RequestParam(value="month", required=false) String month,
			@RequestParam(value="page", required=false, defaultValue="1") Integer pageIndex, Model model){
		Pageable pageable = new PageRequest(pageIndex - 1, 12, new Sort(Direction.DESC, "accessAt"));
		Page<AccessItem> page = null;
		if(month == null){
			page = accessRep.findByUsernameAndServiceAndStatus(pageable, id, productId, 1);
		}else{
			page = accessRep.findByUsernameAndServiceAndMonthAndStatus(pageable, id, productId, month, 1);
		}
		model.addAttribute("page", page);
		model.addAttribute("month", month);
		return "admin/account/visit-detail.jsp";
	}
	
	@RequestMapping(value = "/visit/monthly")
	public String monthly(@RequestParam("id") int id, @RequestParam("productId") Integer productId, Model model){
		String q = "select ai.service, ai.month, count(ai) from AccessItem ai where status = 1 and ai.username = '"
			+ id +"' and ai.service = " 
			+ productId + " group by ai.month order by ai.month desc";
		model.addAttribute("list", em.createQuery(q).getResultList());
		model.addAttribute("user", em.find(Client.class, id));
		model.addAttribute("products", Product.values());
		return "admin/account/visit-month.jsp";
	}
	
	@RequestMapping(value = "/visit/sum")
	public String visits(@RequestParam("id") int id, Model model){
		String q = "select ai.service, count(ai) from AccessItem ai where status = 1 and ai.username = '"+id+"' group by ai.service";
		model.addAttribute("list", em.createQuery(q).getResultList());
		model.addAttribute("user", em.find(Client.class, id));
		model.addAttribute("products", Product.values());
		return "admin/account/visit-sum.jsp";
	}
	
	@RequestMapping(value = "/urls")
	public String urls(@RequestParam("token") String token, HttpServletRequest request, Model model){
		String prex = "http://chachexinxi.cn/chinesecar/api/";
		List<ProductAPI> list = new ArrayList<ProductAPI>();
		list.add(new ProductAPI(prex, token, "&area=828&brand=3&model=25&build=2005&volume=2400&mileage=75000", Product.GANJI_PRICE));
		list.add(new ProductAPI(prex, token, "&brand=3&model=25", Product.IMAGES));
		
		model.addAttribute("list", list);
		return "admin/account/urls.jsp";
	}
	
}
